﻿picShare.service('albumsSvc', function (httpRequestSvc, userDataSvc, albumsAndUserDataSvc, $rootScope,safeApply) {
    return {
        loadUserAlbums: function () {
            httpRequestSvc.getJson(httpRequestSvc.httpRootImageUrl + "load-users-albums?userName=" + userDataSvc.user.userName).done(
                function (data) {
                    albumsAndUserDataSvc.usersAlbumsData = [];

                    for (var i = 0; i < data.length; i++) {

                        var curretnAlbumNames = [];
                        var currnetAlbum = data[i].albumNames;

                        if (currnetAlbum == null) {
                            albumsAndUserDataSvc.usersAlbumsData.push({ userName: data[i].nickName, albumNames: [{ name: "No Albums" }] });
                            continue;
                        }

                        for (var j = 0; j < currnetAlbum.length; j++) {
                            var obj = {
                                name: currnetAlbum[j]
                            };
                            curretnAlbumNames.push(obj);
                        }

                        var currentUsrWithAblumNames = {
                            userName: data[i].nickName,
                            albumNames: curretnAlbumNames
                        };

                        albumsAndUserDataSvc.usersAlbumsData.push(currentUsrWithAblumNames);
                    }
                    safeApply($rootScope);
                   // $rootScope.$apply();//INPORTANT FOR DATABINDING
                },
                function (data) {
                    alert(data);
                });
        }
    };
});